<?php
/**
 * 《外币兑换》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */

require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Convert extends FinanceUtil {
	public $tbl = Constant::tbl_cw_convert;
		
	public $account2 = array(
		'-' => '请选择',
		'1' => '景荣汇丰',		
		'4' => '景荣工行',
		'5' => '中国银行',
		'6' => '招商银行',
		
		'7' => '景荣招行',		
		'2' => '景和汇丰',
		'3' => '景荣南洋',

		'8' => '现金',
		'9' => '其它',
	);
	
	public $bank = array(
		'-' => '请选择',
		'1' => '现金',
		'2' => '汇丰银行',
		'3' => '中国工商银行',
		'4' => '中信银行',
		'5' => '招商银行',
		'6' => '北京银行',
		'7' => '中国民生银行',

		'15' => '中国银行',
		'16' => '南洋商业银行',

		'9' => '其它',
	);
	
	public $account = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '景荣工业集团',
		'3' => '景荣深圳',
		'4' => '广东景荣',
		'5' => '鸿景投资',
		'6' => '滨海中国',
		'7' => '滨海五金厂',
		'8' => '景荣控股',

		'15' => '景和香港',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待兑换',
		'3' => '取消兑换',
		'4' => '完成兑换',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'date' => '日期',
		'rate' => '汇率',
			
		'from_amount' => '外币金额',
		'to_amount' => '本币金额',
			
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'office' => '办公区',
		'account' => '户口',
			
		'from_currency' => '外币',
		'to_currency' => '本币',
		'status' => '状态',
			
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct();
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('bank_option', $this->bank);
		$controller->assign_by_ref('account2_option', $this->account2);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '负责人',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);
		
		$controller->assign('t1', strtotime("-3 month"));

		$controller->display($request, 'finance/convert/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$name_s = $request['name_s'];
		$bank_s = $request['bank_s'];
		$account_s = $request['account_s'];

		$from_currency_s = $request['from_currency_s'];
		$to_currency_s = $request['to_currency_s'];
		
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		$where .= $this->_where_x("bank", $request['bank_s']);
		$where .= $this->_where_x("account", $request['account_s']);
		
		if (!empty($from_currency_s) && $from_currency_s <> '-') {
			$where .= " AND from_currency = '" . $from_currency_s . "'";
		}
		
		if (!empty($to_currency_s) && $to_currency_s <> '-') {
			$where .= " AND to_currency = '" . $to_currency_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND content like '%" . $name_s . "%'";
		}
		
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'149', '2'=>'150', '3'=>'173');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
				
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'finance/convert/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'finance/convert/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'finance/convert/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$bank = $request['bank'];
		$account = $request['account'];
		
		$from_currency = $request['from_currency'];
		$from_amount = $request['from_amount'];
				
		$to_currency = $request['to_currency'];
		$to_amount = $request['to_amount'];
		
		$rate = $request['rate'];
		$date = $request['date'];

		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, from_currency, from_amount, to_currency, to_amount, rate, date, bank, account, content, emp, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $from_currency, $from_amount, $to_currency, $to_amount, $rate, $date, $bank, $account, $content, $emp, $_SESSION['id'], $status);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/convert/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
		
		$bank = $request['bank'];
		$account = $request['account'];
		
		$from_currency = $request['from_currency'];
		$from_amount = $request['from_amount'];
				
		$to_currency = $request['to_currency'];
		$to_amount = $request['to_amount'];
		
		$rate = $request['rate'];
		$date = $request['date'];

		$content = addslashes($request['content']);
		$status = $request['status'];
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', from_currency = '%s', from_amount = '%s', to_currency = '%s', to_amount = '%s', rate = '%s', date = '%s', bank = '%s', account = '%s', ";
		$fmt .= "content = '%s', emp = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $from_currency, $from_amount, $to_currency, $to_amount, $rate, $date, $bank, $account, $content, $emp, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/convert/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['account_t'] = $this->account[$row['account']];
		
		$row['from_currency_t'] = $this->currency[$row['from_currency']];
		$row['to_currency_t'] = $this->currency[$row['to_currency']];
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/convert/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/convert/copy');
		return true;
	}
	
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}
			
			$row = $this->id($this->tbl, $id);			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}
		
		if ($row['status'] > 2) {
			return false;
		}

		if ($this->_audit('77')) {
			return true;
		}

		if ($row['emp'] == $_SESSION['id'] || $row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按外币',
			'2' => '按本币',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'finance/convert/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'account');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(date, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(date, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(date, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(to_amount) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, SUM(from_amount) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'account') {
			$row['a'] = $this->account[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function t($controller, $request) {
		include(APP_LIB . '/simple_html_dom.php');
		
		$url = 'http://www.boc.cn/sourcedb/whpj/';
		$html = file_get_html($url);		
		// echo $html . "\n";
		
		$a = array(
			'英镑' => 'GBP',
			'港币' => 'HKD',
			'美元' => 'USD',
			'日元' => 'JPY',			
			'澳大利亚元' => 'AUD',
			'欧元' => 'EUR',
		);
		
		$ret = array();
		$i = 0;
		foreach($html->find('td[bgcolor=#FFFFFF]') as $e) {
			$s = trim($e->innertext);
			// echo $s . "\n";
			
			if ($i == 8) {
				$ret[] = $row;
				
				$row = array();
				$i = 0;
			}
			
			if ($i == 0 && array_key_exists($s, $a)) {
				$row[] = $a[$s];
			}
			
			$row[] = $s;
			$i++;
		}
		
		// print_r($ret);
		
		$db = newdb('emp');
		while (list($k, $row) = each($ret)) {
			if (count($row) < 9) {
				continue;
			}
			
			// print_r($row);
			
			$fmt = "UPDATE %s SET data = '%s', modifytime = now() WHERE type = 1 AND code = '%s'";
			$sql = sprintf($fmt, Constant::tbl_tec, $row[2] / 100, $row[0]);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function z($controller, $request) {

		$a = array(
			'英镑' => 'GBP',
			'港币' => 'HKD',
			'美元' => 'USD',
			'日元' => 'JPY',			
			'澳大利亚元' => 'AUD',
			'欧元' => 'EUR',
		);
		
		echo $a['欧元'];
		
		logdate($controller, $request);
		return true;
	}
	
	function aa($s) {
		
		
		return $a[$s];
	}
}
?>